package com.gsy.controller.student;


import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gsy.aop.annotation.ParameterValid;
import com.gsy.aop.annotation.ResubmitData;
import com.gsy.aop.annotation.TokenValid;
import com.gsy.entity.LeaveEntity;
import com.gsy.entity.UserEntity;
import com.gsy.enumerate.SystemStatusEnum;
import com.gsy.request.AddLeaveRequest;
import com.gsy.request.NullRequest;
import com.gsy.response.BaseResponse;
import com.gsy.service.LeaveService;
import com.gsy.service.UserService;
import com.gsy.utils.DateUtil;
import com.gsy.vo.GetLeaveVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author gsy
 * @since 2022-03-03
 */
@RestController
@RequestMapping(consumes = "application/json", produces = "application/json")
@Api(value = "controller", tags = {"tb_leave 操作接口"})
public class LeaveController {

    private final static Logger LOGGER = LoggerFactory.getLogger(LeaveController.class);

    @Resource
    private LeaveService leaveService;

    @Resource
    private UserService userService;

//    /**
//    * 更新
//    * @param request
//    * @return
//    */
//    @ApiOperation(value = "更新tb_leave信息", notes = "根据请求参数信息更新tb_leave")
//    @PutMapping("leave")
//    @ParameterValid(apiName = "更新tb_leave信息", formatObj = UpdateLeaveRequest.class)
//    public BaseResponse updateLeaveInformation(@RequestBody UpdateLeaveRequest request) {
//
//        LeaveEntity entity = leaveService.getById(request.getId());
//        if(entity == null){
//            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "不存在该条信息！");
//        }
//        BeanUtils.copyProperties(request, entity);
//
//        Boolean flag = leaveService.updateById(entity);
//        if (flag) {
//            return new BaseResponse(SystemStatusEnum.SUCCESS);
//        } else {
//            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "更新失败！");
//        }
//    }


    /**
     * 撤回请假条
     *
     * @param lId
     * @return
     */
    @ApiOperation(value = "删除tb_leave信息", notes = "根据请求参数信息删除tb_leave信息")
    @GetMapping("users/deleteLeave/{lId}")
    @TokenValid
    public BaseResponse deleteLeave(@PathVariable Integer lId) {
        LeaveEntity entity = leaveService.getById(lId);

        if (entity == null) {
            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "不存在该条信息！");
        }
        if (entity.getState() != 0) {
            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "未审批的请假条才可以撤回！");
        }

        leaveService.removeById(lId);
        return new BaseResponse(SystemStatusEnum.SUCCESS);

    }


    /**
     * 请假申请
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "请假申请", notes = "请假申请")
    @PostMapping("students/setLeave")
    @ParameterValid(apiName = "请假申请", formatObj = AddLeaveRequest.class)
    @TokenValid
    @ResubmitData
    public BaseResponse addLeave(@RequestBody AddLeaveRequest request) {

        LeaveEntity entity = new LeaveEntity();
        BeanUtils.copyProperties(request, entity);
        entity.setStarttime(DateUtil.strToDate(request.getStarttime()));
        entity.setEndtime(DateUtil.strToDate(request.getEndtime()));

        UserEntity userEntity = userService.getById(request.getuId());
        entity.setClasses(userEntity.getClasses());

        boolean flag = leaveService.save(entity);
        if (flag) {
            return new BaseResponse(SystemStatusEnum.SUCCESS);
        } else {
            return new BaseResponse(SystemStatusEnum.FAIL.getCode(), "申请失败！");
        }
    }

}
